package com.ifly.voice.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.os.storage.StorageManager;

public class DicHelp
{

	static final String SD_TYPE = "sd";
	static final String DATA_TYPE = "data";
	String type = SD_TYPE;

	public DicHelp(Context context)
	{
		this.context = context;
	}

	public void startCopy()
	{
		if (type.equals(SD_TYPE))
		{
			Logs.d("to SD_TYPE");
			startCopyToSd();
		}
		else
		{
			Logs.d("to DATA_TYPE");
			startCopyToData();
		}

	}

	private boolean startCopyToData()
	{
		File filesDir = context.getFilesDir();

		setFileDes(filesDir.getAbsolutePath());
		Logs.d("fileDes=" + fileDes);
		try
		{
			AssetManager assets = context.getAssets();

			ArrayList<String> excutefiles = getExcutefiles(assets);

			boolean validateFileIntegrity = validateFileIntegrity(excutefiles);
			Logs.d("validateFileIntegrity=" + validateFileIntegrity);
			setTargetFile(fileDes + "/voice_data");
			if (validateFileIntegrity)
			{
				return true;
			}
			else
			{
				Logs.d("start copy");
				for (String fileName : excutefiles)
				{
					InputStream in = assets.open(fileName);
					copyFile(fileDes + "/" + fileName, in, true);
					Logs.d(fileDes + "/" + fileName);
				}
				return true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return false;
	}

	public static boolean copyFile(String destFileName, InputStream in, boolean isisOverLay)
	{

		File destFile = new File(destFileName);
		if (destFile.exists())
		{
			if (isisOverLay)
			{

			}
		}
		else
		{
			if (!destFile.getParentFile().exists())
			{
				if (!destFile.getParentFile().mkdirs())
				{
					return false;
				}
			}
		}

		int byteread = 0;
		OutputStream out = null;

		try
		{
			out = new FileOutputStream(destFile);
			byte[] buffer = new byte[1024];

			while ((byteread = in.read(buffer)) != -1)
			{
				out.write(buffer, 0, byteread);
			}
			return true;
		}
		catch (FileNotFoundException e)
		{
			return false;
		}
		catch (IOException e)
		{
			return false;
		}
		finally
		{
			try
			{
				if (out != null)
					out.close();
				if (in != null)
					in.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	private String fileDes;
	private String targetFile;
	private Context context;

	public String getTargetFile()
	{
		return targetFile;
	}

	public void setTargetFile(String targetFile)
	{
		this.targetFile = targetFile;
	}

	public void setFileDes(String fileDes)
	{
		this.fileDes = fileDes;
	}

	public boolean startCopyToSd()
	{
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
		{
			File path = Environment.getExternalStorageDirectory();

			setFileDes(path.getAbsolutePath());
			Logs.d("fileDes=" + fileDes);
			try
			{
				AssetManager assets = context.getAssets();

				ArrayList<String> excutefiles = getExcutefiles(assets);

				boolean validateFileIntegrity = validateFileIntegrity(excutefiles);
				Logs.d("validateFileIntegrity=" + validateFileIntegrity);
				setTargetFile(fileDes + "/voice_data");
				if (validateFileIntegrity)
				{
					return true;
				}
				else
				{
					Logs.d("start copy");
					for (String fileName : excutefiles)
					{
						InputStream in = assets.open(fileName);
						copyFile(fileDes + "/" + fileName, in, true);
						Logs.d(fileDes + "/" + fileName);
					}
					return true;
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		return false;
	}

	private ArrayList<String> getExcutefiles(AssetManager assets) throws IOException
	{
		ArrayList<String> excutefiles = new ArrayList<String>();
		excutefiles.add("voice_data/3377.dic");
		excutefiles.add("voice_data/3377.lm");

		String[] list = assets.list("voice_data/tdt_sc_8k");
		for (String string : list)
		{
			excutefiles.add("voice_data/tdt_sc_8k/" + string);
		}
		return excutefiles;
	}

	private boolean validateFileIntegrity(ArrayList<String> excutefiles)
	{
		for (String excutefile : excutefiles)
		{
			if (fileDes.endsWith("/") || fileDes.endsWith("\\"))
			{
				boolean exists = new File(fileDes + excutefile).exists();
				if (!exists)
				{
					return false;
				}
			}
			else
			{
				boolean exists = new File(fileDes + "/" + excutefile).exists();
				if (!exists)
				{
					return false;
				}
			}

		}
		return true;
	}

}
